Actors in Scala (1140) by Philipp Haller & Frank Sommers

Actors in Scala (1140) by Philipp Haller & Frank Sommers

Author:Philipp Haller & Frank Sommers
Language: eng
Format: epub
Tags: Scala
Publisher: Artima Press
Published: 2011-03-14T16:00:00+00:00


val crasher = actor { println("I'm (re-)born") var cnt = 0 loop { cnt += 1 react { case 'request => println("I try to service a request") if (cnt % 2 == 0) { println("sometimes I crash...") throw new Exception } case 'stop => exit() } } }

val client = actor { react { case 'start => for (_ <- 1 to 6) { crasher ! 'request } crasher ! 'stop } }

actor { self.trapExit = true link(crasher) client ! 'start keepAlive(crasher) }

Listing 6.5 - Using keepAlive to automatically restart a crashed actor.

def renderImages(url: String) { val imageInfos = scanForImageInfo(url) self.trapExit = true val dataFutures = for (info <- imageInfos) yield { val loader = link { react { case Download(info) => throw new Exception("no connection") reply(info.downloadImage()) }: Unit } loader !! Download(info) } var i = 0 loopWhile (i < imageInfos.size) { i += 1 val Input = dataFutures(i-1).inputChannel react { case Input ! (data @ ImageData(_)) => renderImage(data) case Exit(from, UncaughtException(_, Some(Download(info)), _, _, cause)) => println("Couldn't download image "+info+ " because of "+cause) } } }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.